home *** CD-ROM | disk | FTP | other *** search
- /* randomize.c
- vi:ts=3 sw=3:
- */
-
- /* $Id: randomize.c,v 4.7 1995/02/08 13:14:56 espie Exp $
- * $Log: randomize.c,v $
- * Revision 4.7 1995/02/08 13:14:56 espie
- * *** empty log message ***
- *
- * Revision 4.7 1995/02/08 13:14:56 espie
- * *** empty log message ***
- *
- * Revision 4.6 1995/02/01 20:41:45 espie
- * Added color.
- *
- * Revision 4.6 1995/02/01 20:41:45 espie
- * Added color.
- *
- * Revision 4.5 1995/02/01 16:39:04 espie
- * *** empty log message ***
- *
- * Revision 4.5 1995/02/01 16:39:04 espie
- * *** empty log message ***
- *
- */
-
- /* input: a series of names (as argv[1:argc - 1])
- * output: the same names, in a random order.
- * with the new database lookup facility, very useful for e.g.,
- * tracker `randomize *` (jukebox)
- */
-
- #include "defs.h"
-
- ID("$Id: randomize.c,v 4.7 1995/02/08 13:14:56 espie Exp $")
-
- /* n = random_range(max): output a number in the range 0:max - 1.
- * For our purpose, we don't have to get a very random number,
- * so the standard generator is alright.
- */
- int random_range(max)
- int max;
- {
- static init = 0;
-
- /* initialize the generator to an appropriate seed eventually */
- if (!init)
- {
- srand(time(0));
- init = 1;
- }
- return rand()%max;
- }
-
- /* output(s): output s in a suitable format. Ideally, output() should use
- * the shell quoting conventions for difficult names. Right now, it doesn't
- */
- void output(s)
- char *s;
- {
- for(; *s; s++)
- switch(*s)
- {
- /* case ' ':
- case '(':
- case ')':
- case '\\':
- putchar('\\');
- */
- default:
- putchar(*s);
- }
- putchar(' ');
- }
-
- int main(argc, argv)
- int argc;
- char *argv[];
- {
- int i, k;
-
- /* set up everything so that our names are in argv[0 : argc - 2] */
- for (i = argc - 1, argv++; i; i--)
- {
- /* invariant: the remaining names are in argv[0: i - 1] */
- k = random_range(i);
- output(argv[k]);
- argv[k] = argv[i - 1];
- }
- exit(0);
- }
-